<?php
class ProductController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column1';

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->layout='//layouts/column2';
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));

	}

	/**
	 * Lists all models.
	 */
	public function actionIndex($string = '', $os='', $size='', $category='')
	{
		// Filters
		$criteria = new CDbCriteria();
	    if( strlen( $string ) > 0 ){
	        $criteria->addSearchCondition( 'name', $string, true, 'OR' );
	        $criteria->addSearchCondition( 'description', $string, true, 'OR' );
	    }
	    if( strlen( $os ) > 0 )
        	$criteria->addCondition( 'os_id='.$os );
        if( strlen( $size ) > 0)
        {
        	switch($size)
        	{
        		case 10:
	        		$criteria->addBetweenCondition('size',10,20);
        			break;
        		case 9:
	        		$criteria->addBetweenCondition('size',9,10);
        			break;
        		case 8:
	        		$criteria->addBetweenCondition('size',8,9);
        			break;
        		case 7:
	        		$criteria->addBetweenCondition('size',7,8);
        			break;
        		default:
	        		$criteria->addBetweenCondition('size',0,7);
        			break;
        	}
        }
        if( strlen( $category ) > 0 )
        {
        	if ($category==0) {
        		$criteria->addCondition( 'category_id<>0');
        	}
        	else 
        		$criteria->addCondition( 'category_id='.$category);
        }

		$dataProvider=new CActiveDataProvider('Product', array(
			'criteria' => $criteria,
			'pagination'=>array(
				'pageSize'=>Yii::app()->params['productPageSize'],
			),
		));

		$oss = Os::model()->findAll();
		$this->render('index',array(
			'oss'=>$oss,
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	/*public function actionIndex()
	{
		$model=new Product('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Product']))
			$model->attributes=$_GET['Product'];

		$this->render('index',array(
			'model'=>$model,
		));
	}*/

	public function genCoverPicture($pictureId, $name, $size)
	{
		$picture = ProductPicture::model()->findByPk($pictureId);
		return CHtml::image(Yii::app()->baseUrl."/uploads/product/".$picture->name.".".$picture->ext, $name, array('width'=>$size));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Product::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='product-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

}
